package com.tn.tgt.easybuy.web;

import com.tn.tgt.easybuy.model.EbProduct;
import com.tn.tgt.easybuy.model.EbProductCategory;
import com.tn.tgt.easybuy.service.EbProductCategoryService;
import com.tn.tgt.easybuy.service.EbProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller//迎宾证
@RequestMapping("/product")
public class EbProductController {

    @Autowired
    private EbProductService ebProductService;

    @Autowired
    private EbProductCategoryService ebProductCategoryService;

    @RequestMapping("/detail")
    public String detail(Model model, Integer epId) {
        System.out.println("=====浏览器点击了某个商品======:epId==" + epId);
        EbProduct productDetail = ebProductService.detail(epId);
        //1.1.调用service层查询商品分类信息
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryService.selectBigAndSmallCate();
        //2.把数据绑定到model
        model.addAttribute("detail",productDetail);
        //2.1 将商品分类进行绑定
        model.addAttribute("categoryMap",categoryMap);
        //3.返回视图名称
        return "product-view";//templates/product-view.html
    }

    @RequestMapping("/cate_product_list/{cateId}")
    public  String productList(Model model,
                                @PathVariable("cateId") Integer cateId,
                               @RequestParam(value = "pSize",defaultValue = "1")Integer pSize,
                               @RequestParam(value = "pageSize",defaultValue ="12" )Integer pageSize){
        System.out.println("=====进入productList方法======cateId"+cateId);
        List<EbProduct>products =ebProductService.selectByCateId(cateId,pSize,pageSize);
        Map<String, List<EbProductCategory>> categoryMap = ebProductCategoryService.selectBigAndSmallCate();
        model.addAttribute("products",products);
        model.addAttribute("categoryMap",categoryMap);
        return "product-list";
    }
}